SHARE
TWEET

nb_image_builder

a guest Mar 15th, 2011 215 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2.  
  3. @model_id=(101);
  4. $dhcp_id=100;
  5.  
  6. $vz_pr_path='/var/lib/vz/private/';
  7. $cache_path='/var/cache/netboot/';
  8. $out_path="/var/lib/vz/root/$dhcp_id/var/lib/tftpboot/images/";
  9.  
  10. for($i=0;$model_id[$i];$i++)
  11. {
  12.     $cur_stamp=`stat $vz_pr_path$model_id[$i]/var/lib/apt/extended_states -c %Y`;
  13.     if (! -e $cache_path . $model_id[$i]) #No checkpoint
  14.     {
  15.         write_stamp($model_id[$i]);
  16.         build_img($model_id[$i],$cur_stamp);
  17.     }  
  18.     else
  19.     {
  20.         $last_stamp=`cat $cache_path$model_id[$i]`;
  21.         if ($cur_stamp > $last_stamp)
  22.         {
  23.             write_stamp($model_id[$i]);
  24.             build_img($model_id[$i],$cur_stamp);
  25.         }
  26.     }
  27. }
  28.  
  29.  
  30. sub build_img
  31. {
  32.     my $id=shift(@_);
  33.     my $stamp=shift(@_);
  34.     use File::Copy;
  35.     $name=`vzlist -a|grep $id|awk '{print \$5}'`;
  36.     chomp($name);
  37.    
  38.     @date=localtime($stamp);
  39.     $date[5]+=1900;$date[4]=sprintf("%02d",++$date[4]);$date[3]=sprintf("%02d",$date[3]);$date[2]=sprintf("%02d",$date[2]);$date[1]=sprintf("%02d",$date[1]);
  40.     $date="$date[5]-$date[4]-$date[3]_$date[2]-$date[1]";
  41.    
  42.     mkdir $out_path . $name;
  43.     $img_filename="$out_path$name/$date";
  44.    
  45.     #Stop model
  46.     system("vzctl stop $id");
  47.     #Convert hardlinks
  48.     system("nb_links_breaker $vz_pr_path$model_id[$i]/");
  49.     #Remove /var/lib/cache/apt
  50.     system("rm $vz_pr_path$model_id[$i]/var/cache/apt/archives/*.deb");
  51.     #Make cpio
  52.     system("cd $vz_pr_path$model_id[$i];find | cpio -H newc -o > $img_filename");
  53.     #Start model
  54.     system("vzconvert $id;vzctl start $id");
  55.     #Compress
  56.     system("gzip -9 $img_filename");
  57.     #Update link "current"
  58.     system("cd $out_path$name;rm current;ln -s $date.gz current");
  59. }
  60.  
  61. sub write_stamp
  62. {
  63.     my $id=shift(@_);
  64.     `stat $vz_pr_path$model_id[$i]/var/lib/apt/extended_states -c %Y > $cache_path$model_id[$i]`;
  65. }
RAW Paste Data
Top